package com.bto.solar.work.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.bto.solar.work.config.TimestampStringConverter;
import com.bto.solar.work.dao.IConstructService;
import com.bto.solar.work.dao.ICustomerService;
import com.bto.solar.work.dao.IDesignService;
import com.bto.solar.work.dao.IDeviceService;
import com.bto.solar.work.dao.IGridConnectionService;
import com.bto.solar.work.dao.IInsuranceService;
import com.bto.solar.work.dao.ILeaseContractService;
import com.bto.solar.work.dao.IOrderBaseService;
import com.bto.solar.work.dao.IOrderStateService;
import com.bto.solar.work.dao.IReconnoitreService;
import com.bto.solar.work.dao.IRecordService;
import com.bto.solar.work.dao.ISettlementService;
import com.bto.solar.work.entity.Customer;
import com.bto.solar.work.entity.OrderBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 王小波
 * @description:   订单信息excel导出
 * @date 2023/9/11 8:29
 */
@Service
public class BtoExcelService {
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IOrderBaseService orderService;
    @Autowired
    private IReconnoitreService reconnoitreService;
    @Autowired
    private IDesignService designService;
    @Autowired
    private IOrderStateService orderStateService;
    @Autowired
    private IConstructService constructService;
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private IRecordService recordService;
    @Autowired
    private ILeaseContractService leaseContractService;
    @Autowired
    private IGridConnectionService gridConnectionService;
    @Autowired
    private ISettlementService settlementService;
    @Autowired
    private IInsuranceService insuranceService;
    /**
    *  批量导出订单excel
    */
    public void batchExportOrderInfo(List<String> orderIds , List<String> columns  , HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easy-excel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream())
                .registerConverter(new TimestampStringConverter())
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .sheet("模板")
                .head(getExcelHead(columns)).doWrite(getOrderDataByColumns(orderIds , columns));
    }

    private List<List<Object>> getOrderDataByColumns(List<String> orderIds , List<String> columns){
        List<List<Object>> orderData = new ArrayList<List<Object>>();
        orderIds.forEach(orderId -> {
            OrderBase orderBase = orderService.getById(orderId);
            Customer customer = customerService.getById(orderBase.getCustomerId());

            //TODO 根据需要导出的列，添加数据
            List<Object> objectList = new ArrayList<>();
            if(columns.contains("订单编号")){
                objectList.add(orderBase.getOrderId());
            }
            if(columns.contains("订单类型")){
                objectList.add(orderBase.getType());
            }
            if(columns.contains("客户姓名")){
                objectList.add(customer.getName());
            }
            if(columns.contains("客户电话")){
                objectList.add(customer.getPhone());
            }

            orderData.add(objectList);
        });

        return orderData;

    }


    private List<List<String>> getExcelHead(List<String> columns){
        List<String> head0 = null;
        List<List<String>> list = new LinkedList<List<String>>();
        for (String h : columns) {
            head0 = new LinkedList<>();
            head0.add(h);
            list.add(head0);
        }
        return list;
    }


}
